package test.id;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang3.StringUtils;

public class IdGenerator {
	
	// "yyyyMMddHHmmss" + "C" + "2位" + "N" + 3位
	
	AtomicInteger seed = new AtomicInteger(Integer.MAX_VALUE - 3);
	
	public String gen() {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String id = sdf.format(new Date());
		id = id + "C02";
		
		int num = seed.incrementAndGet();
		long n = Integer.MAX_VALUE  + (long)num + 1;
		String s = StringUtils.right(String.valueOf(n), 3);
		s = StringUtils.leftPad(s, 3, "0");
		
		id = id + s;
		return id;
		
	}
	
	public static void main(String[] args) throws InterruptedException {
		IdGenerator generator = new IdGenerator();
		
		for (int i = 0; i < 100; i++) {
			Thread.sleep(100);
			System.out.println(generator.gen());
		}
	}

}
